<?php
namespace app\admin\controller;

use app\common\component\message\MessageService;

class Message extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

	public function index() {
		$this -> view -> assign('messages', (new MessageService) -> getAll());

		return $this -> view -> fetch();
	}

	public function update() {
		if($this -> request -> isPost()) {
			$post = $this -> request -> post();
			$post['reply_time'] = time();

			$resultMessage = (new MessageService) -> update($post, $this -> request -> get('message_id'));
			if($resultMessage['code'] !== 0) {
				echo '<meta charset="utf-8"><script>alert(' . $resultMessage['message'] . ');window.history.go(-1);</script>';exit;
			}
			echo '<meta charset="utf-8"><script>window.location.href="/shuashuale/message";</script>';exit;
		}
		$resultMessage = (new MessageService) -> getMessage($this -> request -> get('message_id'));
		$this -> view -> assign('message', $resultMessage['data']['message']);

		return $this -> view -> fetch();
	}

	public function delete() {
		$resultMessage = (new MessageService) -> delete($this -> request -> get('message_id'));
		if($resultMessage['code'] !== 0) {
			echo '<meta charset="utf-8"><script>alert(' . $resultMessage['message'] . ');window.history.go(-1);</script>';exit;
		}
		echo '<meta charset="utf-8"><script>window.location.href="/shuashuale/message";</script>';exit;
	}
}